package no.hig.imt4721.matching;

import java.security.InvalidParameterException;

import no.hig.imt4721.model.Repetition;
import no.hig.imt4721.model.Template;
import no.hig.imt4721.model.TemplateWithAverageDeviation;

public class DistanceMetricMaximumAverageDeviation extends DistanceMetric
{
	public double getDistance(Template template, Repetition repetition)
	{	
		if(!(template instanceof TemplateWithAverageDeviation))
			throw new InvalidParameterException();
		
		TemplateWithAverageDeviation templateWithSD = (TemplateWithAverageDeviation) template;
		
		
		double max = 0;
		
		for(int i = 0; i < repetition.getLengthOfRow(); i++)
		{
			double d = getSingleFeatureDistance(template.getValue(i), repetition.getRowElement(i));
			
			double averageDeviation = templateWithSD.getAverageDeviationValue(i);
			
			
			if(d / averageDeviation > max)
				max = d / averageDeviation;
		}
		
		return max;
	}
	
	
	private double getSingleFeatureDistance(double inputValue, double templateValue)
	{
		return Math.abs(inputValue - templateValue);
	}
}